package com.zzx.threekingdom.judge;

import com.zzx.threekingdom.card.BaseCard;
import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.EquipmentCard;
import com.zzx.threekingdom.card.KnowhowCard;
import com.zzx.threekingdom.card.KnowhowCard.KnowhowCardType;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.ActionInfo;

import java.util.ArrayList;


public class Rule
{
    
    public enum PlayerStep
    {
        turnBegin, judge, receiveCards, useCards, abandonCards, turnEnd
    }
    
    
    
    public static PlayerStep getNextStep(PlayerStep ps)
    {
        switch(ps)
        {
            case turnBegin: return PlayerStep.judge;
            case judge: return PlayerStep.receiveCards;
            case receiveCards: return PlayerStep.useCards;
            case useCards: return PlayerStep.abandonCards;
            case abandonCards: return PlayerStep.turnEnd;
            case turnEnd: return PlayerStep.turnBegin;
        }
        return ps;
    }
    
    public static String getStepName(PlayerStep ps)
    {
        switch(ps)
        {
            case turnBegin: return "回合开始";
            case judge: return "判定";
            case receiveCards: return "摸牌";
            case useCards: return "出牌";
            case abandonCards: return "弃牌";
            case turnEnd: return "回合结束";
        }
        return "";
    }
    
    
    public static class CardRule
    {
        public static boolean isCardActiveUse(Card card, AbsPlayerModel userPm)
        {
            return (isCardActiveUseByNoPlayer(card, userPm)==true)||(isCardActiveUseByTargetPlayer(card, userPm)==true);
        }
        
        public static boolean isCardActiveUseByNoPlayer(Card card, AbsPlayerModel userPm)
        {
            if(card instanceof EquipmentCard)
            {
                return true;
            }
            else if(card instanceof KnowhowCard)
            {
                switch(((KnowhowCard) card).getKonwhowType())
                {
                    case lightning:
                    case harvest:
                    case outofnothing:
                    case allpeach:
                    case alieninvade:
                    case swarmofarrows:
                        return true;
                }
            }
            
            return false;
        }
        
        public static boolean isCardActiveUseByTargetPlayer(Card card, AbsPlayerModel userPm)
        {
            if(card instanceof BaseCard)
            {
                if((((BaseCard)card).getType()==BaseCard.BaseCardType.kill) 
                        && ((userPm.getHasUsedKill()==false)||(userPm.hasEquipment("诸葛连弩")==false)))
                    return true;
            }
            else if(card instanceof KnowhowCard)
            {
                switch(((KnowhowCard) card).getKonwhowType())
                {
                    case leaveallbehind:
                    case getknifetokill:
                    case duel:
                    case steal:
                    case remove:
                        return true;
                    case lightning:
                        return (userPm.getJudgeByName("闪电") == null);
                }
            }
            return false;
        }
        
        public static int geTargetNumByActionName(String actionName)
        {
            if(actionName==null||actionName.length()==0)
                return 0;
            if(actionName.equals("杀")||actionName.equals("乐不思蜀")||actionName.equals("决斗")
                ||actionName.equals("顺手牵羊")||actionName.equals("过河拆桥"))
                return 1;
            else if(actionName.equals("借刀杀人"))
                return 2;
            return 0;
        }
        
        public static int getCardActiveUseTargetNum(Card card)
        {
            if(card != null)
                return geTargetNumByActionName(card.getName());
            return 0;
        }
        
        public static ArrayList<AbsPlayerModel> getTargetByActionName(AbsPlayerModel pm, String actionName)
        {
        	
        	
        	if(actionName == null || actionName.length() == 0)
        		return new ArrayList<AbsPlayerModel>();
        	
        	
        	if(actionName.equals("杀"))
        	{
        		return pm.getPmInAttackRange();
        	}
        	else if(actionName.equals("乐不思蜀"))
        	{
        	    ArrayList<AbsPlayerModel> ret = Judge.judge.getLivePmFromeMe(pm);
        	    for(int i = 0;i < ret.size();i++)
                {
                    AbsPlayerModel target = ret.get(i);
                    if(target.getJudgeByName(actionName) != null)
                        ret.remove(i--);
                }
        	}
        	else if(actionName.equals("决斗"))
        	{
        		return Judge.judge.getLivePmFromeMe(pm);
        	}
        	else if(actionName.equals("顺手牵羊") || actionName.equals("过河拆桥"))
        	{
        	    ArrayList<AbsPlayerModel> ret = pm.getPmInRange(pm.getDistanceFromMe());
        		for(int i = 0;i < ret.size();i++)
        		{
        			AbsPlayerModel target = ret.get(i);
        			if(target.hasCard()==false)
        				ret.remove(i--);
        		}
        	}
        	else if(actionName.equals("借刀杀人"))
        	{
        		//return Judge.judge.getLivePmFromeMe(pm);
        	}
        	return new ArrayList<AbsPlayerModel>();
        }
        
        public static boolean isImmediateKnowhowType(KnowhowCardType type)
        {
            switch(type)
            {
                case impeccable:
                case getknifetokill:
                case harvest:
                case outofnothing:
                case duel:
                case allpeach:
                case alieninvade:
                case swarmofarrows:
                case steal:
                case remove:
                    return true;
                default:
                    return false;
            }
        }
        public static boolean isImmediateKnowhowCard(KnowhowCard card) { return isImmediateKnowhowType(card.getKonwhowType()); }
        public static boolean isDelayKnowhowType(KnowhowCardType type) { return !isImmediateKnowhowType(type); }
        public static boolean isDelayKnowhowCard(KnowhowCard card) { return !isImmediateKnowhowCard(card); }
        public static boolean isImmediateKnowhowString(String cardName) { return isImmediateKnowhowType(KnowhowCard.string2Type(cardName)); }
        public static boolean isDelayKnowhowString(String cardName) { return !isImmediateKnowhowString(cardName); }
    }
    
    private Rule() {}

}
